<template>
  <div class="login-container">
    <div class="form-container" :class="{ 'is-register': isRegister }">
      <!-- 登录表单面板 -->
      <div class="form-panel sign-in">
        <h2>欢迎回来</h2>
        <el-form :model="loginForm" ref="loginFormRef" :rules="rules" class="form-content">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="Lock" type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-button type="primary" :loading="loading" @click="handleLogin" class="submit-btn">登录</el-button>
        </el-form>
      </div>

      <!-- 注册表单面板 -->
      <div class="form-panel sign-up">
        <h2>创建账号</h2>
        <el-form :model="registerForm" ref="registerFormRef" :rules="rules" class="form-content">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" prefix-icon="User" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password" prefix-icon="Lock" type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" prefix-icon="Lock" type="password" placeholder="请确认密码" />
          </el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister" class="submit-btn">注册</el-button>
        </el-form>
      </div>

      <!-- 覆盖层 -->
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h2>已有账号？</h2>
            <p>请使用您的账号登录系统</p>
            <el-button class="ghost" @click="toggleForm">去登录</el-button>
          </div>
          <div class="overlay-panel overlay-right">
            <h2>没有账号？</h2>
            <p>立即注册加入我们</p>
            <el-button class="ghost" @click="toggleForm">去注册</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'
import request from '../axios/request'

const userStore = useUserStore()
const router = useRouter()
const isRegister = ref(false)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const toggleForm = () => {
  isRegister.value = !isRegister.value
}

const handleLogin = async () => {
  loading.value = true
  try {
    const { access_token, user } = await request.post('/admin/login', loginForm)
    console.log('登录成功:', access_token, user)
    userStore.setToken(access_token)
    userStore.setUserInfo(user)
    ElMessage.success('登录成功')
    router.push('/home/dashboard')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

const handleRegister = async () => {
  loading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('注册成功')
    isRegister.value = false
  } catch (error) {
    ElMessage.error('注册失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #00416A 0%, #E4E5E6 100%);
  padding: 20px;
}

.form-container {
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
  width: 900px;
  max-width: 100%;
  min-height: 580px;
}

.form-panel {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
  padding: 50px;
  width: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.sign-in {
  left: 0;
  z-index: 2;
}

.sign-up {
  left: 0;
  opacity: 0;
  z-index: 1;
}

.form-content {
  width: 100%;
  max-width: 320px;
}

.form-container.is-register .sign-in {
  transform: translateX(100%);
}

.form-container.is-register .sign-up {
  transform: translateX(75%);
  opacity: 1;
  z-index: 5;
  animation: show 0.6s;
}

@keyframes show {
  0%, 49.99% {
    opacity: 0;
    z-index: 1;
  }
  50%, 100% {
    opacity: 1;
    z-index: 5;
  }
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.form-container.is-register .overlay-container {
  transform: translateX(-100%);
}

.overlay {
  position: relative;
  color: #fff;
  background: linear-gradient(to right, #00416A, #2563EB);
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.form-container.is-register .overlay {
  transform: translateX(50%);
}

.overlay-panel {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 40px;
  height: 100%;
  width: 50%;
  text-align: center;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-right {
  right: 0;
  transform: translateX(0);
}

.overlay-left {
  transform: translateX(-20%);
}

.form-container.is-register .overlay-left {
  transform: translateX(0);
}

.form-container.is-register .overlay-right {
  transform: translateX(20%);
}

h2 {
  font-size: 2rem;
  margin-bottom: 1.5rem;
  color: #2c3e50;
}

.overlay h2 {
  color: #fff;
}

p {
  margin: 1rem 0 2rem;
  font-size: 1rem;
  color: rgba(255, 255, 255, 0.9);
}

.ghost {
  background: transparent;
  border: 2px solid #fff;
  color: #fff;
  margin: 0.5rem;
  transition: all 0.3s ease;
}

.ghost:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.submit-btn {
  width: 100%;
  padding: 12px;
  margin-top: 1rem;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

:deep(.el-input__wrapper) {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 12px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

@media (max-width: 768px) {
  .form-container {
    min-height: 500px;
  }

  .form-panel {
    padding: 30px 20px;
    width: 100%;
  }

  .sign-up {
    left: 100%;
    opacity: 1;
  }

  .overlay-container {
    display: none;
  }

  .form-container.is-register .sign-in {
    transform: translateX(-100%);
  }

  .form-container.is-register .sign-up {
    transform: translateX(-100%);
  }
}
</style>